#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "code.h"




void main(){
    char r[] = "./config.txt";
    char w[] = "./code.txt";
    char d[] = "./decode.txt";
    // codeFile(r, w);

    decodeFile(w, d);
}



// 文件加密
void codeFile(char * src, char *dest){
    FILE * file_s = fopen(src,"r");
    FILE * file_d = fopen(dest,"w");

    if(!file_s || !file_d){
        printf("file open faild\n");
        return;
    }

    char ch;
    while( (ch=fgetc(file_s))!=EOF){
        short temp = (short)ch;

        temp = temp<< 4;
        temp = temp | 0x8000;
        temp += rand() % 16;

        fprintf(file_d, "%hd", temp); // 

    }

    fclose(file_s);
    fclose(file_d);

}

// 解密
void decodeFile(char * src, char *dest){
    FILE * file_s = fopen(src,"r");
    FILE * file_d = fopen(dest,"w");

    if(!file_s || !file_d){
        printf("file open faild\n");
        return;
    }

    short temp;

    while( !feof(file_s) ){
        fscanf(file_s,"%hd", &temp);

        temp  = temp<<1;
        temp = temp>>5;

        char ch = (char)temp;
        fputc(ch,file_d);
    }
    fclose(file_s);
    fclose(file_d);

}